<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ScanPhoto;
use App\Admin\Extensions\Tools\ImportExcel;
use App\Admin\Extensions\Tools\ScanList;
use App\Models\School;
use App\Models\Student;
use App\Handlers\FacePhotoHandler;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class StudentsController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('学生信息表');
            $content->description('关于学生的详细信息');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑学生的基本信息');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新增学生信息');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        return Admin::grid(Student::class, function (Grid $grid) {
            Admin::script(<<<EOF
        function scanToFace(id){
        $.post('/api/scanface/'+id);
        toastr.warning('正在识别，请稍后...', '');
        }
EOF
            );
            $grid->tools(function ($tools) {
                $tools->append(new ScanList());
                $tools->append(new ImportExcel());
            });
            $grid->actions(function ($actions) {

                // 添加操作
                $actions->prepend(new ScanPhoto($actions->getKey(), $actions->row['is_scaned']));
            });
            $grid->id('ID')->sortable();
            $grid->column('num', '学号');
            $grid->column('school.name','所属学校');
            $grid->column('name', '名字');
            $grid->column('age', '年龄');
            $grid->column('sex', '性别')->display(function ($v){
               return $v ? '男':'女';
            });
            $grid->column('is_scaned', '是否已识别')->display(function ($isScaned) {
                return $isScaned ? '是' : '否';
            });
            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Student::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('num', '学号');
            $form->select('school_id', '所属学校')->options(School::all()->pluck('name','id'));
            $form->text('name', '姓名');
            $form->radio('sex', '性别')->options([0 => '女', 1=> '男']);
            $form->number('age', '年龄');
            $form->divide();
            $form->image('photo', '用于脸部识别的照片')->uniqueName();
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
